package model;

import com.sun.org.apache.xpath.internal.operations.Or;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class OrderDao {

    //创建在shoporder表里面创建新的订单
    public int createShopOrder(int userId){

        //与数据库建立连接
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        try {
            try {
                Class.forName("com.mysql.jdbc.Driver");
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
            //1.建立连接
            connection = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/taobao?characterEncoding=utf8&serverTimezone=Asia/Shanghai&useSSL=false","root","dddd");
            System.out.println("开始创建新的订单");
            //2.具体实现
            String sql = "insert into shoporder values (null,?,now(),null)";
            statement = connection.prepareStatement(sql);
            statement.setInt(1,userId);
            statement.executeUpdate();

            String sql1 = " select max(orderId) as orderId from shoporder;";
            statement = connection.prepareStatement(sql1);
            resultSet = statement.executeQuery();

            if (resultSet.next()){
                return resultSet.getInt("orderId");
            }


        } catch (SQLException e) {
            e.printStackTrace();
        }



        return 0;
    }

    //往orderdetail表中插入商品信息
    public void insertShop(int orderId, List<Shop> shops){

        //与数据库建立连接
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        try {
            try {
                Class.forName("com.mysql.jdbc.Driver");
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
            //1.建立连接   ?characterEncoding=utf8&serverTimezone=Asia/Shanghai&useSSL=false
            connection = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/taobao?characterEncoding=utf8&serverTimezone=Asia/Shanghai&useSSL=false","root","dddd");
            System.out.println("开始插入订单商品");
            //2.将shops中的数据全部插入orderdetail表中
            for (int i =0;i<shops.size();i++){
                String sql = "insert into orderdetail values (?,?,?,?,?,?,?)";
                statement = connection.prepareStatement(sql);
                statement.setInt(1,orderId);
                statement.setInt(2,shops.get(i).getShopId());
                statement.setString(3,shops.get(i).getCategory());
                statement.setString(4,shops.get(i).getIcon());
//                statement.setString(5,shops.get(i).getShopName());
                statement.setString(5,shops.get(i).getShopName());
                statement.setString(6,shops.get(i).getShopBrief());
                statement.setDouble(7,shops.get(i).getPrice());
                statement.executeUpdate();
                System.out.println("插入第"+i+"个商品");
            }



            //3.获取orderId的订单总价格
            String sql1 = "select sum(price) as sum from orderdetail where orderId = ?";
            statement = connection.prepareStatement(sql1);
            statement.setInt(1,orderId);
            resultSet = statement.executeQuery();
            Double sumPrice = null;
            if (resultSet.next()){
                sumPrice = resultSet.getDouble("sum");
            }
            System.out.println("订单总价格:"+sumPrice);
            //3.更新订单总价格
            String sql2 = "update shoporder set sumPrice = ? where orderId = ?";
            statement = connection.prepareStatement(sql2);
            statement.setDouble(1,sumPrice);
            statement.setInt(2,orderId);
            statement.executeUpdate();
            //更新订单总价格成功
            System.out.println("更新订单总价格成功");





        } catch (SQLException e) {
            e.printStackTrace();
        }



    }

    //获取shoporder表中的所以订单
    public List<Order> selectAllOrder(int userId){
        List<Order> orderList = new ArrayList<>();
        //与数据库建立连接
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;

        try {
            try {
                Class.forName("com.mysql.jdbc.Driver");
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
            //1.建立连接
            connection = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/taobao?characterEncoding=utf8&serverTimezone=Asia/Shanghai&useSSL=false","root","dddd");
            System.out.println("查询订单项准备完成");
            //2.查找订单数据
            String sql = "select * from shoporder where userId = ?";
            statement = connection.prepareStatement(sql);
            statement.setInt(1,userId);
            resultSet = statement.executeQuery();
            //3.将查询到的数据放入Order对象，然后再放入orderList集合
            while (resultSet.next()){
                Order order = new Order();
                order.setOrderId(resultSet.getInt("orderId"));
                order.setTime(String.valueOf(resultSet.getTime("time")));
                order.setSumPrice(resultSet.getDouble("sumPrice"));
                orderList.add(order);
            }
            System.out.println("查询订单成功");
            return orderList;

        } catch (SQLException e) {
            e.printStackTrace();
        }


        System.out.println("查询订单失败");
        return null;
    }


    //根据orderID获取订单商品信息
    public List<Shop> selectAllOrderShop(int orderId){
        List<Shop> shops = new ArrayList<>();
        //与数据库建立连接
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;

        try {
            try {
                Class.forName("com.mysql.jdbc.Driver");
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
            //1.建立连接
            connection = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/taobao?characterEncoding=utf8&serverTimezone=Asia/Shanghai&useSSL=false","root","dddd");
            System.out.println("查询订单项准备完成");
            //2.查找订单数据
            String sql = "select * from orderdetail where orderId = ?";
            statement = connection.prepareStatement(sql);
            statement.setInt(1,orderId);
            resultSet = statement.executeQuery();
            //3.将查询到的数据放入Order对象，然后再放入orderList集合
            while (resultSet.next()){
              Shop shop = new Shop();
              shop.setShopId(resultSet.getInt("shopId"));
              shop.setCategory(resultSet.getString("category"));
              shop.setIcon(resultSet.getString("icon"));
              shop.setShopName(resultSet.getString("shopName"));
              shop.setShopBrief(resultSet.getString("shopBrief"));
              shop.setPrice(resultSet.getDouble("price"));
              shops.add(shop);
            }
            System.out.println("查询订单成功");
            return shops;

        } catch (SQLException e) {
            e.printStackTrace();
        }


        System.out.println("查询订单失败");
        return null;

    }




}
